home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Science⁄Math / VideoToolbox / Demos / TestAssign.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-16  |  4.4 KB  |  142 lines  |  [TEXT/KAHL]

  1. /*
  2. TestAssign.c
  3.  
  4. This is significantly more complicated than it needs to be. The fancy stuff
  5. is merely to put Assign.c through its paces. In particular, if the array a[]
  6. didn't change dimensions from block to block in testAssign1 then we could
  7. read the whole file by a single call to ReadAssignmentFile, instead of
  8. opening the file and making repeated calls to ReadAssignmentBlock, etc.
  9.  
  10. For a good example of the use of Assign, see ReadLuminanceRecord.c.
  11.  
  12. HISTORY:
  13. 8/93    dgp    created this new version, which cobbles together several older versions.
  14. 8/12/93    dgp    updated to work with new Assign.c
  15. 9/8/93    dgp    call Require() and use new AllocateDescriptions.
  16. */
  17. #include "VideoToolbox.h"
  18. #include <assert.h>
  19. #if MAC_C
  20.     #include "Luminance.h"
  21. #endif
  22. #define PREFERENCES_FOLDER 0
  23. void moreTests(void);
  24. void main(void);
  25.  
  26. typedef struct {
  27.     double *a,logC,viewingDistance;
  28.     long trials;
  29.     char *message;
  30.     unsigned char *m;
  31. } Psychophysics;
  32. Description *DescribePsychophysics(Psychophysics *p);
  33.  
  34. Description *DescribePsychophysics(Psychophysics *p)
  35. {
  36.     Description *d;
  37.     int i;
  38.     
  39.     d=AllocateDescriptions(10);
  40.     i=0;
  41.     d[i++]=Describe(stringType,&p->message,"message",NULL);
  42.     d[i++]=Describe(doubleType,&p->viewingDistance,"viewingDistance",NULL);
  43.     d[i++]=Describe(shortType,&p->trials,"trials",NULL);
  44.     d[i++]=Describe(doubleType,&p->logC,"logC",NULL);
  45.     d[i++]=Describe(unsignedCharPtrType,&p->m,"m",NULL);
  46.     d[i++]=Describe(doublePtrType,&p->a,"a",NULL);
  47.     assert(i<=10);        /*  make sure array was big enough */
  48.     d[i++]=NullDescription();        /*  mark end of array */
  49.     return d;
  50. }
  51.  
  52. void main(void)
  53. {
  54.     Description *d;
  55.     static Psychophysics psychophysics,*p;
  56.     short i,j,flags=assignReportUnknown;
  57.     FILE *stream;
  58.     char filename[]="testAssign1";
  59.     
  60.     Require(0);
  61.     p=&psychophysics;
  62.     d=DescribePsychophysics(p);
  63.     InitializeDescribedVars(d,flags);
  64.     stream=fopen(filename,"r");
  65.     do{
  66.         printf("\n/******** New block ********/\n");
  67.         ReadAssignmentBlock(stream,d,flags);
  68.             /* a real program would do something useful here with the data in p */
  69.             /* instead we fool around with print statements */
  70.             PrintAssignments(stdout,d,flags);
  71.             printf("Now dimensioned as a");
  72.             for(i=0;;i++){
  73.                 j=FindDescribedDim(d,&p->a,i,flags);
  74.                 if(j<=0)break;
  75.                 printf("[%d]",j);
  76.             }
  77.             printf(".\n");
  78.         FreeDescribedPtrVars(d,flags);
  79.     } while(!feof(stream));
  80.     fclose(stream);
  81.     FreeDescribedVars(d,flags);
  82.     FreeDescriptions(d);
  83.     if(0)moreTests();
  84. }
  85. void moreTests(void)
  86. {
  87.     static double logC,maskLogC,viewingDistance,*a,conditionLogC[4],conditionMaskLogC[4];
  88.     short trials,i,flags;
  89.     static char *kindOfTrial,*message,image[1000];
  90.     unsigned char *m;
  91.     Description *d;
  92.     FILE *stream;
  93.     static char filename[]="testAssign2";
  94.     short error;
  95.     static double b[2][2],c[1][2][3];
  96.     
  97.     flags=assignReportUnknown;
  98.     
  99.     printf("\n\n\nMore tests.\n");
  100.     #ifdef _LUMINANCE_
  101.         if(1){
  102.             static luminanceRecord LR;
  103.             
  104.             // Test ReadLuminanceRecord.c
  105.             if(PREFERENCES_FOLDER)error=OpenPreferencesFolder();
  106.             i=ReadLuminanceRecord("LuminanceRecord1.h",&LR,flags);
  107.             if(PREFERENCES_FOLDER)error=ClosePreferencesFolder();
  108.             if(i<=0)PrintfExit("Couldn't read LuminanceRecord, error %d.\n",i);
  109.             i=WriteLuminanceRecord("LR.h",&LR,flags);
  110.             if(i<=0)PrintfExit("Couldn't write/verify LuminanceRecord, error %d.\n",i);
  111.             printf("ReadLuminanceRecord.c seems to work fine. Created LR.h\n\n");
  112.         }
  113.     #endif
  114.     #if 1
  115.         /* parse testAssign2 */
  116.         d=AllocateDescriptions(14);
  117.         i=0;
  118.         d[i++]=Describe(doublePtrType,&a,"a","array");
  119.         d[i++]=Describe(unsignedCharPtrType,&m,"m","chars");
  120.         d[i++]=DescribeArray(charType,&image,"image","Image data",100L,0L);
  121.         d[i++]=Describe(stringType,&message,"message",NULL);
  122.         d[i++]=Describe(doubleType,&logC,"logC","log contrast");
  123.         d[i++]=Describe(doubleType,&maskLogC,"maskLogC",NULL);
  124.         d[i++]=Describe(stringType,&kindOfTrial,"kindOfTrial",NULL);
  125.         d[i++]=Describe(doubleType,&viewingDistance,"viewingDistance","inches");
  126.         d[i++]=Describe(shortType,&trials,"trials",NULL);
  127.         d[i++]=DescribeArray(doubleType,&b,"b",NULL,2L,2L,0L);
  128.         d[i++]=DescribeArray(doubleType,&c,"c",NULL,1L,2L,3L,0L);
  129.         assert(i<=14);                                /* make sure array was big enough */
  130.         d[i++]=NullDescription();                    /* mark end of array */
  131.         InitializeDescribedVars(d,0);
  132.         stream=fopen(filename,"r");
  133.         ReadAssignmentLine(stream,d
  134.             ,assignHexFloats|assignReportUnknown|assignEchoAssignments);
  135.         FreeDescribedVars(d,0);
  136.         fclose(stream);
  137.         ReadAssignmentFile(filename,d,assignHexFloats|assignReportUnknown);
  138.         PrintAssignments(stdout,d,0);
  139.         FreeDescriptions(d);
  140.     #endif
  141. }
  142.